<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background-color: #fff;
    }
    .error_icon,
    .success_icon {
      display: inline-block;
      vertical-align: middle;
      width: 20px;
      height: 20px;
      background: url(./image/svg/error.svg) no-repeat;
      margin-top: -2px;
    }
    .success {
      color: green;
    }
    .success_icon {
      background: url(./image/svg/success.svg) no-repeat;
    }
    .container {
      padding: 40px;
      border-radius: 20px;
      border: 8px solid #fff;
      box-shadow: -5px -5px 15px rgba(255, 255, 255, 0.1),
      5px 5px 15px rgba(0, 0, 0, 0.35),
      inset -5px -5px 15px rgba(255, 255, 255, 0.1),
      inset 5px 5px 15px rgba(0, 0, 0, 0.35)
    }
    .container .form {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      gap: 25px;
    }
    .container .form h2 {
      color: black;
      font-weight: 500;
      letter-spacing: 0.1em;
    }
    .container .form .inputBox {
      position: relative;
      width: 300px;
    }
    .container .form .inputBox input {
      padding: 12px 10px 12px 48px;
      border: none;
      width: 100%;
      /* background: #223243; */
      border: 1px solid rgba(0, 0, 0, 0.1);
      color: black;
      font-weight: 300;
      border-radius: 25px;
      font-size: 1em;
      box-shadow: -5px -5px 15px rgba(255, 255, 255, 0.1),
      5px 5px 15px rgba(0, 0, 0, 0.35);
      transition: 0.5s;
      outline: none;
    }
    .container .form .inputBox span {
      position: absolute;
      left: 0;
      padding: 12px 10px 12px 48px;
      pointer-events: none;
      font-size: 1em;
      font-weight: 300;
      transition: .5s;
      letter-spacing: 0.05em;
      /* color: rgba(225, 225, 225, 0.5); */
    }
    .container .form .inputBox img {
      position: absolute;
      top: 15px;
      left: 16px;
      width: 25px;
      padding: 2px 0;

    }
    /* 验证通过 :invalid-->验证未通过 */
    .container .form .inputBox input:valid ~ span,
    .container .form .inputBox input:focus ~ span
    {
      color: #00dfc4;
      border: 1px solid #00dfc4;
      /* background: #223243; */
      transform: translateX(25px) translateY(-7px);
      font-size: 0.6em;
      padding: 0.8px;
      border-radius: 10px;
      letter-spacing: 0.1em;
    }
    .container .form .inputBox input:valid:not(:last-child) ,
    .container .form .inputBox input:focus:not(:last-child) 
    {
      border: 1px solid #00dfc4;
    }
    .container .form .inputBox input[type="submit"] {
      background: #ff0f5b;
      color: #fff;
      text-transform: uppercase;
      cursor: pointer;
      letter-spacing: 0.1em;
      font-weight: 500;
      padding: 10px 0;
      cursor: pointer;
      box-shadow: -5px -5px 15px rgba(255, 255, 255, 0.1),
      5px 5px 15px rgba(0, 0, 0, 0.35),
      inset -5px -5px 15px rgba(12, 12, 12, 0.1),
      inset 5px 5px 15px rgba(0, 0, 0, 0.35)
    }
    .container .form p {
      color: rgba(255, 255, 255, 0.5);
      font-size: 0.75em;
      font-weight: 300;
    }
    .container .form p a {
      font-weight: 500;
      color: #fff;
    }
    .agree {
      width: 300px;
      /* display: list-item; */
    /* padding-left: 95px; */
  }
  .agree input {
    vertical-align: middle;
  }
  .agree a {
    color: #1ba1e6;
  }
  </style>
</head>
<body>
  <div class="container">
    <div class="form signup">
      <h2>Sign Up</h2>
      <div class="inputBox">
        <input type="text" required="required" name="" id="username">
        <span class="">请输入用户名</span>
        <img src="./image/svg/resUser.svg" alt="">
        <!-- <span>用户名</span> -->
      </div>
      <div class="inputBox">
        <input type="password" required="required" name="" id="password">
        <span class="">请输入密码</span>
        <img src="./image/svg/password.svg" alt="">
        <!-- <span>密码</span> -->
      </div>
      <div class="inputBox">
        <input type="password" required="required" name="" id="dpassword">
        <span class="">确认密码</span>
        <img src="./image/svg/password.svg" alt="">
        <!-- <span>确认密码</span> -->
      </div>
      <div class="inputBox">
        <input type="text" required="required" maxlength="11" name="" id="phone">
        <span class="">请输入手机号</span>
        <img src="./image/svg/phone.svg" alt="">
        <!-- <span>手机号</span> -->
      </div>
      <div class="inputBox">
        <input type="text" required="required" name="" id="email">
        <span class="">请输入邮箱</span>
        <img src="./image/svg/email.svg" alt="">
        <!-- <span>邮箱</span> -->
      </div>
      <div class="agree">
        <input type="checkbox" name="" id="">同意协议并注册
        <a href="#">《同意用户协议》</a>
      </div>
      <div class="inputBox">
        <input type="submit" required="required" value="创建账户" name="" id="">
      </div>
      <p>已有账户？<a href="">登录</a></p>
    </div>
  </div>
</body>
<script>
  window.onload = function() {
    // 手机号
    const regPho = /^1[3|4|5|7|8]\d{9}$/;
    // 用户名
    const regName = /^[\u4e00-\u9fa5]{2,8}$/;
    // 密码
    const regPsw = /^[a-zA-z0-9_-]{6,16}$/;
    // 邮箱
    const regEmail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
    const phoneEle = document.querySelector('#phone')
    const userEle = document.querySelector('#username')
    const pswEle = document.querySelector('#password')
    const dpswEle = document.querySelector('#dpassword')
    const emailEle = document.querySelector('#email')
    regInput(phoneEle, regPho);
    regInput(userEle, regName)
    regInput(pswEle, regPsw)
    regInput(emailEle, regEmail)
    // console.log(pswEle.value);
    // 表单验证函数
    function regInput(ele, reg) {
        ele.onblur = function () {
            if (this.value !== '') {
                if (reg.test(this.value)) {
                    // console.log('正确的');
                    this.nextElementSibling.className = 'success';
                    this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
                } else {
                    // console.log('不正确');
                    this.nextElementSibling.className = 'error';
                    this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 格式不正确，请重新输入';
                }
            } else {
                this.nextElementSibling.innerHTML = '请输入内容';
            }
        };
    };
    // 两次密码验证
    dpswEle.onblur = function () {
        if (this.value !== '') {
            if (this.value === pswEle.value) {
                this.nextElementSibling.className = 'success';
                this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
            } else {
                this.nextElementSibling.className = 'error';
                this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 两次密码输入不一致';
            }
        } else {
            this.nextElementSibling.innerHTML = '请输入内容';
        }
    };
  }
</script>
</html>